package com.gosuncn.config.pdf;

import com.gosuncn.utils.IpHelperUtil;
import org.springframework.beans.factory.FactoryBean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * 工厂类
 */
public class HtmlToPdfFactoryBean implements FactoryBean<AbstractHtmlToPdf> {

    @Override
    public AbstractHtmlToPdf getObject() throws Exception {
        String osname = IpHelperUtil.getSystemOSName();

        if (IpHelperUtil.isWindows(osname)) {
            return new WindowsHtmlToPdf();
        }
        // ARM架构 linux 处理
        Process process = Runtime.getRuntime().exec("command -v wkhtmltopdf");
        try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            String line = br.readLine();
            System.out.println(">>>>>>>>>>>>>>>>>>>" + line);
            if (line != null && !"".equals(line) && !"null".equals(line)) {
                return new ARMHtmlToPdf();
            }
        } catch (IOException e) {
            throw e;
        }
        if (IpHelperUtil.isLinux(osname)) {
            return new LinuxHtmlToPdf();
        }
        if (IpHelperUtil.isMac(osname)) {
            return new LinuxHtmlToPdf();
        }
        return null;
    }

    @Override
    public Class<?> getObjectType() {
        return AbstractHtmlToPdf.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

}
